package com.example.neusoft.a16110100126yttordering.activities;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.a16110100126yttordering.R;
import com.example.neusoft.a16110100126yttordering.bean.LoginBean;
import com.example.neusoft.a16110100126yttordering.listener.RetrofitListener;
import com.example.neusoft.a16110100126yttordering.model.UserModel;

public class LoginActivity extends BaseActivity implements RetrofitListener<LoginBean> {
    private EditText username,userpass;
    private Button loginButton;
    private Button registerButton;
    private Button phoneregisterButton;
    private TextView textView;
    private ImageView imageView;


    @Override
    void initViews() {
        setContentView(R.layout.activity_login);
        username=findViewById(R.id.editText1);
        userpass=findViewById(R.id.editText2);
        loginButton=findViewById(R.id.button1);
        registerButton=findViewById(R.id.button3);
        phoneregisterButton=findViewById(R.id.button4);
        textView=findViewById(R.id.textView1);
        imageView=findViewById(R.id.imageView9);
    }

    @Override
    void initEvents() {
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
        phoneregisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,PhoneRegisterActivity.class);
                startActivity(intent);
            }
        });
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent=new Intent();
//                intent.setClass(LoginActivity.this,MainActivity.class);
//                startActivity(intent);
                if(username.getText().toString().trim().equals("")||userpass.getText().toString().trim().equals("")){
                    Toast.makeText(LoginActivity.this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    login();
                }
            }
        });
    }
    public void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        data=getIntent();
        String et_user=data.getStringExtra("user");
        System.out.println(et_user+"ytt------------lalalalalla");
        if(et_user!=null){
            username.setText(et_user);
        }
    }
    @Override
    void initData() {

    }
     void login(){
        UserModel userModel=new UserModel();
        userModel.userLogin(username.getText().toString().trim(),userpass.getText().toString().trim(),this);
    }

    @Override
    public void onSuccess(LoginBean userId, int flag) {
        if (userId.getUserid().equals("0")) {
            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        } else {
            saveUser(userId.getUserid());
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onFail() {
        Toast.makeText(LoginActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
    }
    public void saveUser(String user_id){
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();//获取编辑器
        editor.putString("username",username.getText().toString());
        int userid=Integer.parseInt(user_id);
        editor.putInt("user_id",userid);
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();//提交修改
    }
}
